package com.rhb.rhbaiagent.tools;

import cn.hutool.core.io.FileUtil;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

public class PDFParsingTool {

    @Tool(description = "Parse PDF file and extract text content")
    public String parsePDF(@ToolParam(description = "Path to the PDF file to parse") String pdfPath) {
        try {
            // 检查文件是否存在
            File pdfFile = new File(pdfPath);
            if (!pdfFile.exists()) {
                return "Error: PDF file not found at path: " + pdfPath;
            }

            // 使用PDFBox解析PDF
            try (PDDocument document = Loader.loadPDF(pdfFile)) {
                PDFTextStripper pdfStripper = new PDFTextStripper();
                String text = pdfStripper.getText(document);
                
                // 如果文本为空
                if (text == null || text.trim().isEmpty()) {
                    return "Warning: No text content found in the PDF file.";
                }
                
                return "PDF content extracted successfully:\n\n" + text.trim();
            }
        } catch (IOException e) {
            return "Error parsing PDF: " + e.getMessage();
        } catch (Exception e) {
            return "Unexpected error occurred while parsing PDF: " + e.getMessage();
        }
    }

    @Tool(description = "Parse PDF file and extract text content with page range")
    public String parsePDFWithRange(
            @ToolParam(description = "Path to the PDF file to parse") String pdfPath,
            @ToolParam(description = "Start page number (1-based)") int startPage,
            @ToolParam(description = "End page number (1-based)") int endPage) {
        try {
            File pdfFile = new File(pdfPath);
            if (!pdfFile.exists()) {
                return "Error: PDF file not found at path: " + pdfPath;
            }

            try (PDDocument document = Loader.loadPDF(pdfFile)) {
                PDFTextStripper pdfStripper = new PDFTextStripper();
                
                // 设置页面范围
                if (startPage > 0 && endPage >= startPage && endPage <= document.getNumberOfPages()) {
                    pdfStripper.setStartPage(startPage);
                    pdfStripper.setEndPage(endPage);
                } else {
                    return "Error: Invalid page range. PDF has " + document.getNumberOfPages() + " pages.";
                }
                
                String text = pdfStripper.getText(document);
                
                if (text == null || text.trim().isEmpty()) {
                    return "Warning: No text content found in the specified page range.";
                }
                
                return "PDF content extracted from pages " + startPage + "-" + endPage + ":\n\n" + text.trim();
            }
        } catch (IOException e) {
            return "Error parsing PDF: " + e.getMessage();
        } catch (Exception e) {
            return "Unexpected error occurred while parsing PDF: " + e.getMessage();
        }
    }
}